package com.pdstudio.weather;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.pdstudio.app.Application;
import com.pdstudio.app.Application.EventHandler;
import com.pdstudio.bean.LiveIndex;
import com.pdstudio.http.LiveHttpMethod;
import com.pdstudio.util.SharePreferenceUtil;
import com.pdstudio.view.SwipeBackActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class LiveIndexActivity extends SwipeBackActivity implements
		EventHandler {

	private GridView gvLiveIndex;
	private SimpleAdapter liveSimpleAdapter;
	private View customLiveIndexTitleView;
	private TextView txtTitle;
	private ImageView ivBack;
	private SharePreferenceUtil mSpUtil;
	private Application mApplication;
	//生活指数数据
	private List<Map<String, Object>> mLiveIndexData;

	private final int MESSAGE_GETLIVEINDEXLIST = 0;

	private Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MESSAGE_GETLIVEINDEXLIST:
				if (msg != null && msg.obj != null) {
					@SuppressWarnings("unchecked")
					List<LiveIndex> data = (List<LiveIndex>) msg.obj;
					convertLiveIndexData(data);
				}
				if (mLiveIndexData.size() == 0) {
					Toast.makeText(LiveIndexActivity.this, "当前无生活指数数据",
							Toast.LENGTH_SHORT).show();
				} else {
					setLiveIndexData(mLiveIndexData);
				}
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_live_index);

		Application.mListeners.add(this);
		mApplication = Application.getInstance();
		mSpUtil = mApplication.getSharePreferenceUtil();

		LinearLayout layout = (LinearLayout) findViewById(R.id.activity_live_index);
		int resId = mApplication.getApplicationBackground(mSpUtil
				.getWeatherStatus());
		layout.setBackground(getResources().getDrawable(resId));

		customLiveIndexTitleView = findViewById(R.id.title_liveindex);
		txtTitle = (TextView) customLiveIndexTitleView
				.findViewById(R.id.title2_text_nav);
		txtTitle.setText("生活指数");

		ivBack = (ImageView) findViewById(R.id.title2_back);
		ivBack.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				LiveIndexActivity.this.finish();
			}

		});

		initLiveIndexList();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.live_index, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/*
	 * 转换生活指数数据的图片
	 */
	private void convertLiveIndexData(List<LiveIndex> data) {

		mLiveIndexData = new ArrayList<Map<String, Object>>();
		for (LiveIndex o : data) {
			o.setResourceId(o.getIndexKey());
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("uri", o.getResourceId());
			map.put("desc", o.getIndexKey() + "指数");
			map.put("value", o.getIndexValue());

			mLiveIndexData.add(map);
		}
	}

	/*
	 * 获取生活指数列表
	 */
	@SuppressLint("HandlerLeak")
	private void initLiveIndexList() {

		// 去接口获取实况信息
		try {
			Thread thread = new Thread(runnable);
			thread.start();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void setLiveIndexData(List<Map<String, Object>> list) {
		gvLiveIndex = (GridView) findViewById(R.id.gridview_liveindex);
		liveSimpleAdapter = new SimpleAdapter(this, list,
				R.layout.gridview_item_liveindex, new String[] { "uri", "desc",
						"value" }, new int[] { R.id.item_liveindex_uri,
						R.id.item_liveindex_indexname,
						R.id.item_liveindex_indexvalue });
		gvLiveIndex.setAdapter(liveSimpleAdapter);
	}

	@Override
	public void onCityComplite() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onNetChange() {
		// TODO Auto-generated method stub

	}

	// 开一条子线程加载网络数据
	Runnable runnable = new Runnable() {
		@Override
		public void run() {
			try {
				Thread.sleep(0);

				LiveHttpMethod http = new LiveHttpMethod(LiveIndexActivity.this);
				List<LiveIndex> data = http.GetLiveIndexList(
						mSpUtil.getAuthCode(),
						mApplication.getCurSimpleCity().cityCode);

				Message m = mHandler.obtainMessage(MESSAGE_GETLIVEINDEXLIST,
						data);
				mHandler.sendMessage(m);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	};
}
